package morning.cat.demo1.io3;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.junit.Test;

public class InputStreamTest {

	/**
	 * 读取指定文件内容，按照16进制输出到控制台 并且每输出10个byte换行 单字节读取不适合大文件，大文件效率很低
	 */
	@Test
	public void test1() throws IOException {
		String fileName = "path1\\raf.dat";
		// 把文件作为字节流进行读操作
		FileInputStream in = new FileInputStream(fileName);
		int b;
		int i = 1;
		while ((b = in.read()) != -1) {
			if (b <= 0xf) {
				// 单位数前面补0
				System.out.print("0");
			}
			System.out.print(Integer.toHexString(b) + "  ");
			if (i++ % 10 == 0) {
				System.out.println();
			}
		}
		in.close();
	}

	/**
	 * 批量读取，对大文件而言效率高，也是我们最常用的读文件的方式
	 */
	@Test
	public void test2() throws IOException {
		String fileName = "pom.xml";
		FileInputStream in = new FileInputStream(fileName);

		// 8k
		byte[] buf = new byte[8 * 1024];
		/*
		 * 从in中批量读取字节，放入到buf这个字节数组中， 从第0个位置开始放，最多放buf.length个 返回的是读到的字节的个数
		 */
		/*
		 * int bytes = in.read(buf,0,buf.length);//一次性读完，说明字节数组足够大 int j = 1;
		 * for(int i = 0; i < bytes;i++){
		 * System.out.print(Integer.toHexString(buf[i] & 0xff)+"  ");
		 * if(j++%10==0){ System.out.println(); } }
		 */
		int bytes = 0;
		int j = 1;
		while ((bytes = in.read(buf, 0, buf.length)) != -1) {
			for (int i = 0; i < bytes; i++) {
				System.out.print(Integer.toHexString(buf[i] & 0xff) + "  ");
				if (j++ % 10 == 0) {
					System.out.println();
				}
			}
		}
		in.close();
	}

	/**
	 * 文件拷贝，字节批量读取
	 * 
	 */
	@Test
	public void test3() throws IOException {
		File srcFile = new File("path1\\raf.dat");
		File destFile = new File("path1\\raf-copy.dat");
		//
		FileInputStream in = new FileInputStream(srcFile);
		FileOutputStream out = new FileOutputStream(destFile);
		byte[] buf = new byte[8 * 1024];
		int b;
		while ((b = in.read(buf, 0, buf.length)) != -1) {
			out.write(buf, 0, b);
			out.flush();// 最好加上
		}
		in.close();
		out.close();
	}

	/**
	 * 进行文件的拷贝，利用带缓冲的字节流
	 * 
	 */
	@Test
	public void test4() throws IOException {
		File srcFile = new File("path1\\raf.dat");
		File destFile = new File("path1\\raf-copy.dat");
		//
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
				srcFile));
		BufferedOutputStream bos = new BufferedOutputStream(
				new FileOutputStream(destFile));
		int c;
		while ((c = bis.read()) != -1) {
			bos.write(c);
			bos.flush();// 刷新缓冲区
		}
		bis.close();
		bos.close();
	}

	/**
	 * 单字节，不带缓冲进行文件拷贝
	 * 
	 */
	@Test
	public void test5() throws IOException {
		File srcFile = new File("path1\\raf.dat");
		File destFile = new File("path1\\raf-copy.dat");
		//
		FileInputStream in = new FileInputStream(srcFile);
		FileOutputStream out = new FileOutputStream(destFile);
		int c;
		while ((c = in.read()) != -1) {
			out.write(c);
			out.flush();
		}
		in.close();
		out.close();
	}

	@Test
	public void test6() throws IOException {
		// 如果该文件不存在，则直接创建，如果存在，删除后创建
		FileOutputStream out = new FileOutputStream("path1/out.dat");
		out.write('A');// 写出了'A'的低八位
		out.write('B');// 写出了'B'的低八位
		int a = 10;// write只能写八位,那么写一个int需要些4次每次8位
		out.write(a >>> 24);
		out.write(a >>> 16);
		out.write(a >>> 8);
		out.write(a);
		byte[] gbk = "中国".getBytes("gbk");
		out.write(gbk);
		out.close();
	}

}
